//
//  ZLCellLayoutConfig.m
//  ZLYoYoIM_Example
//
//  Created by 赵磊 on 2021/1/15.
//  Copyright © 2021 itzhaolei. All rights reserved.
//

#import "ZLCellLayoutConfig.h"
#import "ZLAttachment.h"
#import "ZLUnidentificationMessageContentView.h"
#import "ZLSessionCustomTypeHeader.h"

@implementation ZLCellLayoutConfig

- (CGSize)contentSize:(NIMMessageModel *)model cellWidth:(CGFloat)width{
    if ([self canLayout:model]) {
        NIMCustomObject *object = model.message.messageObject;
        ZLAttachment *attachment = object.attachment;
        NSString *type = attachment.type;
        if ([type intValue] == ZLSessionCustomTypeBurnAfterReading) {
            return CGSizeMake(122, 122);
        }
        if ([type intValue] == ZLSessionCustomTypeGift) {
            return CGSizeMake(120.0, 120.0);
        }
        return CGSizeMake(240, 40);
    }
    return [super contentSize:model cellWidth:width];
}

- (NSString *)cellContent:(NIMMessageModel *)model{
    if ([self canLayout:model]) {
        NIMCustomObject *object = model.message.messageObject;
        ZLAttachment *attachment = object.attachment;
        NSString *type = attachment.type;
        if ([type intValue] == ZLSessionCustomTypeBurnAfterReading) {
            return @"ZLBurnAfterReadingCellContentView";
        }
        if ([type intValue] == ZLSessionCustomTypeGift) {
            return @"ZLGiftCellContentView";
        }
        return @"ZLUnidentificationMessageContentView";
    }
    return [super cellContent:model];
}

- (UIEdgeInsets)cellInsets:(NIMMessageModel *)model{
    if ([self canLayout:model]) {
        //填入气泡距cell的边距,选填
        NIMCustomObject *object = model.message.messageObject;
        ZLAttachment *attachment = object.attachment;
        NSString *type = attachment.type;
        if ([type intValue] == ZLSessionCustomTypeBurnAfterReading) {
            return  model.message.isOutgoingMsg ? UIEdgeInsetsMake(0, 5, 15, 5) : UIEdgeInsetsMake(0, 60, 15, 0);
        }
        if ([type intValue] == ZLSessionCustomTypeGift) {
            return  model.message.isOutgoingMsg ? UIEdgeInsetsMake(0, 5, 15, 5) : UIEdgeInsetsMake(0, 60, 15, 0);
        }
        return [super cellInsets:model];
    }
    return [super cellInsets:model];
    
}


- (UIEdgeInsets)contentViewInsets:(NIMMessageModel *)model{
    if ([self canLayout:model]) {
        NIMCustomObject *object = model.message.messageObject;
        ZLAttachment *attachment = object.attachment;
        NSString *type = attachment.type;
        if ([type intValue] == ZLSessionCustomTypeBurnAfterReading) {
            return UIEdgeInsetsMake(0, 0, 0, 5);
        }
        if ([type intValue] == ZLSessionCustomTypeGift) {
            return UIEdgeInsetsMake(0, 0, 0, 5);
        }
        //填入内容距气泡的边距,选填
        return [super contentViewInsets:model];
    }
    return [super contentViewInsets:model];
    
}


- (BOOL)canLayout:(NIMMessageModel *)model
{
    return model.message.messageType == NIMMessageTypeCustom;
}

/**
 * 是否显示气泡背景图
 */
- (BOOL)shouldDisplayBubbleBackground:(NIMMessageModel *)model {
    if ([self canLayout:model]) {
        NIMCustomObject *object = model.message.messageObject;
        ZLAttachment *attachment = object.attachment;
        NSString *type = attachment.type;
        if ([type intValue] == ZLSessionCustomTypeBurnAfterReading) {
            return NO;
        }
        if ([type intValue] == ZLSessionCustomTypeGift) {
            return NO;
        }
        return YES;
    }
    return YES;
}

@end
